package com.ruoyi.web.controller.applet.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.YesOrNo;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.SysAppPrescriptionInfo;
import com.ruoyi.system.domain.SysAppPrescriptionSort;
import com.ruoyi.system.domain.SysAppPrescriptioninfoMedicine;
import com.ruoyi.system.domain.SysDictData;
import com.ruoyi.system.dto.PrescriptionDicDataDTO;
import com.ruoyi.system.dto.PrescriptionSortDTO;
import com.ruoyi.system.service.ISysAppPrescriptionInfoService;
import com.ruoyi.system.service.ISysAppPrescriptionSortService;
import com.ruoyi.system.service.ISysAppPrescriptioninfoMedicineService;
import com.ruoyi.system.service.ISysDictTypeService;

@Controller
@RequestMapping("/applet/prescription")
public class PrescriptionController {

	@Autowired
	private ISysDictTypeService dictTypeService;

	@Autowired
	private ISysAppPrescriptionSortService sysAppPrescriptionSortService;

	@Autowired
	private ISysAppPrescriptionInfoService sysAppPrescriptionInfoService;

	@Autowired
	private ISysAppPrescriptioninfoMedicineService sysAppPrescriptioninfoMedicineService;

	/**
	 * 初始化方剂列表内容
	 * 
	 * @Description:
	 * @Author 豫东一棵草
	 * @DateTime 2020年10月17日 下午11:10:48
	 * @return
	 */
	@PostMapping("/selectPrescriptionInfo")
	@ResponseBody
	public AjaxResult selectPrescriptionInfo(String keyWord) {
		Map<String, Object> map = Maps.newConcurrentMap();
		List<SysDictData> dictDatas = dictTypeService.selectDictDataByType("sys_prescription_sort");
		PrescriptionDicDataDTO prescriptionDicDataDTO = null;
		PrescriptionSortDTO prescriptionSortDTO = null;
		SysAppPrescriptionInfo sysAppPrescriptionInfo = null;
		List<PrescriptionDicDataDTO> prescriptionDicDatas = Lists.newArrayList();
		List<PrescriptionSortDTO> sortDTOs = null;
		for (SysDictData sysDictData : dictDatas) {
			prescriptionDicDataDTO = new PrescriptionDicDataDTO();
			BeanUtils.copyBeanProp(prescriptionDicDataDTO, sysDictData);
			SysAppPrescriptionSort sysAppPrescriptionSort = new SysAppPrescriptionSort();
			// sysAppPrescriptionSort.setSortName(keyWord);
			sysAppPrescriptionSort.setDictLable(sysDictData.getDictCode().toString());
			sysAppPrescriptionSort.setStatus(YesOrNo.yes.getInfo());
			List<SysAppPrescriptionSort> prescriptionSort = sysAppPrescriptionSortService
					.selectSysAppPrescriptionSortList(sysAppPrescriptionSort);
			sortDTOs = Lists.newArrayList();
			for (SysAppPrescriptionSort sort : prescriptionSort) {
				prescriptionSortDTO = new PrescriptionSortDTO();
				BeanUtils.copyBeanProp(prescriptionSortDTO, sort);
				sysAppPrescriptionInfo = new SysAppPrescriptionInfo();
				sysAppPrescriptionInfo.setSortId(sort.getId());
				sysAppPrescriptionInfo.setTitle(keyWord);
				List<SysAppPrescriptionInfo> prescriptionInfo = sysAppPrescriptionInfoService
						.selectInfoBySortIdOrTitle(sysAppPrescriptionInfo);
				if (prescriptionInfo.size() > 0) {
					prescriptionSortDTO.setInfoList(prescriptionInfo);
					sortDTOs.add(prescriptionSortDTO);
				}

			}
			prescriptionDicDataDTO.setSortList(sortDTOs);
			prescriptionDicDatas.add(prescriptionDicDataDTO);
		}
		map.put("prescription", prescriptionDicDatas);
		return AjaxResult.success("操作成功", map);
	}

	/**
	 * 根据分类id获取方剂列表
	 * 
	 * @Description:
	 * @Author 豫东一棵草
	 * @DateTime 2020年10月17日 下午11:20:22
	 * @param sortId
	 * @return
	 */
	@PostMapping("/selectPrescriptionInfoBySortId")
	@ResponseBody
	public AjaxResult selectPrescriptionInfoBySortId(Long sortId) {
		Map<String, Object> map = Maps.newConcurrentMap();
		SysAppPrescriptionInfo sysAppPrescriptionInfo = new SysAppPrescriptionInfo();
		sysAppPrescriptionInfo.setSortId(sortId);
		List<SysAppPrescriptionInfo> prescriptionInfo = sysAppPrescriptionInfoService
				.selectSysAppPrescriptionInfoList(sysAppPrescriptionInfo);

		map.put("prescriptionInfo", prescriptionInfo);
		return AjaxResult.success("操作成功", map);
	}

	/**
	 * 获取方剂详情
	 * 
	 * @Description:
	 * @Author 豫东一棵草
	 * @DateTime 2020年10月17日 下午11:24:04
	 * @param id
	 * @return
	 */
	@PostMapping("/selectPrescriptionInfoById")
	@ResponseBody
	public AjaxResult selectPrescriptionInfoById(Long id) {
		Map<String, Object> map = Maps.newConcurrentMap();

		/* 获取方剂的详情 */
		SysAppPrescriptionInfo prescriptionInfo = sysAppPrescriptionInfoService.selectSysAppPrescriptionInfoById(id);

		/* 获取方剂中的中药信息 */
		SysAppPrescriptioninfoMedicine sysAppPrescriptioninfoMedicine = new SysAppPrescriptioninfoMedicine();
		sysAppPrescriptioninfoMedicine.setPrescriptioninfoId(id);
		List<SysAppPrescriptioninfoMedicine> medicines = sysAppPrescriptioninfoMedicineService
				.selectMedicineList(sysAppPrescriptioninfoMedicine);
		map.put("prescriptionInfo", prescriptionInfo);
		map.put("medicine", medicines);
		return AjaxResult.success("操作成功", map);
	}

}
